
#include "session_manager.h"


session& session_manager::find_session(struct sockaddr_in *client) {
    int session_index = 0;

    for (std::vector<session>::iterator iter_session = m_sessions.begin();
            iter_session != m_sessions.end(); ++iter_session) {
        if ((iter_session->client_addr() == client->sin_addr.s_addr) &&
		    (iter_session->client_port() == client->sin_port)) {
            break;
        }
        else {
            session_index++;
        }
    }

    if (session_index == m_sessions.size()) {
        session new_session(client->sin_port, client->sin_addr.s_addr);
        m_sessions.push_back(new_session);
    }

    return m_sessions[session_index];
}

int session_manager::find_session_index(struct sockaddr_in *client) {
    int session_index = 0;

    for (std::vector<session>::iterator iter_session = m_sessions.begin();
            iter_session != m_sessions.end(); ++iter_session) {
		 if ((iter_session->client_addr() == client->sin_addr.s_addr) &&
		    (iter_session->client_port() == client->sin_port)) {
            break;
        }
        else {
            session_index++;
        }
    }

    return session_index;
}

void session_manager::delete_session(int session_index) {
	m_sessions.erase(m_sessions.begin() + session_index);
	return;
}
